package com.example.demo.juc;

import java.util.concurrent.*;

public class Test04 {

    static CountDownLatch countDownLatch = new CountDownLatch(4);
    static ExecutorService executorService = Executors.newFixedThreadPool(4);


    static class MyThread implements Callable {

        int start;
        int end;

        public MyThread(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public Object call() {
            countDownLatch.countDown();
            int temp = 0;
            for (int i = start; i < end; i++) {
                temp += i;
            }
            return temp;
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        int result = 0;
        for (int i = 0; i <= 4; i++) {
            result += (int) executorService.submit(new MyThread((i - 1) * 25 + 1, i * 25)).get();
        }
        countDownLatch.await();
        System.out.println(result);
        executorService.shutdown();
    }
}
